package com.arm.service.impl;

import com.arm.dao.ShoppingCartMapper;
import com.arm.module.ConsigneeInfo;
import com.arm.module.Order;
import com.arm.module.ShoppingCart;
import com.arm.module.User;
import com.arm.service.BaseService;
import com.arm.service.OrderService;
import com.arm.service.ShoppingCartService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Baggio on 2019-1-14.
 */
@Service
public class ShoppingCartServiceImpl extends BaseService implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    OrderService orderService;

    @Override
    public void deleteShoppingCart(Long tid) {
        shoppingCartMapper.deleteByPrimaryKey(tid);
    }

    @Override
    public void clearShoppingCart(Long userId) {
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public List<ShoppingCart> getShoppingCartListByUserId(Long userId) {
        return shoppingCartMapper.getShoppingCartListByUserId(userId);
    }

    @Override
    public void modifyShoppingCart(ShoppingCart shoppingCart) {
        if (shoppingCart.getTid() == null) {
            throw new MyRunTimeExcption("购物车出错！");

        }
        deleteShoppingCart(shoppingCart.getTid());
        addShoppingCart(shoppingCart);
    }

    @Override
    public void addShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setCreateTime(new Date());
        shoppingCartMapper.insertShoppingCart(shoppingCart);
    }

    @Transactional
    @Override
    public Order payShoppingCart(String shoppingCartIds, User user) {
    /*    String productJsons, User user, ConsigneeInfo consigneeInfo*/

        if (user == null) {
            throw new MyRunTimeExcption("用户不存在！");
        }
        if (user.getTid() == null) {
            throw new MyRunTimeExcption("用户Id不存在！");
        }

        ConsigneeInfo consigneeInfo = new ConsigneeInfo();
        if (StringUtils.isNotBlank(user.getUserName())) {
            consigneeInfo.setConsigneeName(user.getUserName());

        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            consigneeInfo.setConsigneePhone(user.getPhone());

        }
        if (StringUtils.isNotBlank(user.getUserAddress())) {
            consigneeInfo.setConsigneeAddress(user.getUserAddress());

        }
        if (StringUtils.isBlank(shoppingCartIds)) {
            throw new MyRunTimeExcption("没有发现购物车");
        }

        List<Long> idList = new ArrayList<Long>();

        for (String shopCarId : shoppingCartIds.split(",")
                ) {
            idList.add(Long.parseLong(shopCarId));
        }
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShoppingCartListByIds(idList);

        String productJsons = "";
        for (ShoppingCart shoppingCart : shoppingCartList
                ) {
            shoppingCart.getProductEntityId();
            shoppingCart.getAmount();
            String productJson = "{\"productEntityId\":\"" + shoppingCart.getProductEntityId() + "\", \"amount\":" + shoppingCart.getAmount() + "}";
            productJsons += productJson + ",";
            deleteShoppingCart(shoppingCart.getTid());
        }

        productJsons = "["+productJsons.trim().substring(0, productJsons.length() - 1) + "]";

        return orderService.createOrder(productJsons, user, consigneeInfo);
    }
}
